import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.Component;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.TreeSet;
import java.awt.List;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class VueRechercheMotcle extends Vue {
	
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JComboBox comboBox;
	private JLabel searched;
	private List listOuvrages;
	private List listArticles;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane1;
	private JTextArea textArea;
	private MotCle searchMot;
	
	public VueRechercheMotcle(Controleur controleur) {
		super(controleur);
		
		setTitle("Rechercher par mot-cle");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
		setBounds(100, 100, 463, 597);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String searchVal = getText();
				searchMot = getControleur().getMotcle(searchVal);
				if ( searchMot != null ) {
					searched.setText(searchVal.concat(" :"));
					
					listOuvrages.removeAll();
					listArticles.removeAll();
					textArea.setText("");
					textArea.setEnabled(true);
					
					
					for ( Document d : searchMot.getDocuments() ) {	
						if ( d.getClass().getName().contentEquals("Ouvrage") ) {
							listOuvrages.add(d.getTitre());
							listOuvrages.setEnabled(true);
						} else {
							listArticles.add(d.getTitre());
							listArticles.setEnabled(true);
						}
					}
					
				} else if ( searchVal != "" ) {
					searched.setText("");
					if ( searchMot == null ) {
						listOuvrages.setEnabled(false);
						listOuvrages.removeAll();
						listArticles.setEnabled(false);
						listArticles.removeAll();
						textArea.setEnabled(false);
						textArea.setText("");
					}
					TreeSet<String> matchedWords = getControleur().getMots(searchVal);
					if ( matchedWords != null) {
						comboBox.removeAllItems();
						comboBox.addItem(searchVal);
						for ( String s : matchedWords ) {
							comboBox.addItem(s);
						}
						comboBox.setPopupVisible(true);
					}
				}
				
				
			}
		});
		
		comboBox.setForeground(Color.LIGHT_GRAY);
		comboBox.setEditable(true);
		comboBox.setBounds(110, 66, 225, 31);
		contentPane.add(comboBox);
		
		JLabel lblNewLabel = new JLabel("Recherche par mot-clef");
		lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 20));
		lblNewLabel.setBounds(111, 27, 225, 25);
		contentPane.add(lblNewLabel);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(28, 114, 390, 2);
		contentPane.add(separator);
		
		searched = new JLabel("");
		searched.setFont(new Font("Tahoma", Font.BOLD, 14));
		searched.setBounds(38, 127, 390, 27);
		contentPane.add(searched);
		
		scrollPane = new JScrollPane(listOuvrages);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(10, 178, 183, 181);
		contentPane.add(scrollPane);
		
		scrollPane1 = new JScrollPane((Component) null);
		scrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane1.setBounds(254, 178, 183, 181);
		contentPane.add(scrollPane1);
		
		listOuvrages = new List();
		listOuvrages.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent arg0) {
				if ( listArticles.getSelectedIndex() != -1 ) {
					listArticles.deselect(listArticles.getSelectedIndex());
				}
				if ( searchMot != null ) {
					textArea.setText(searchMot.getDocument(listOuvrages.getSelectedItem().toString(), "Ouvrage").afficher());
				}
			}
		});
		listOuvrages.setEnabled(false);
		scrollPane.setViewportView(listOuvrages);
		listArticles = new List();
		listArticles.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent arg0) {
				if ( listOuvrages.getSelectedIndex() != -1 ) {
					listOuvrages.deselect(listOuvrages.getSelectedIndex());
				}
				if ( searchMot != null ) {
					textArea.setText(searchMot.getDocument(listArticles.getSelectedItem().toString(), "Article").afficher());
				}
			}
		});
		listArticles.setEnabled(false);
		scrollPane1.setViewportView(listArticles);
		
		JButton btnRetourAuMenu = new JButton("Retour au menu");
		btnRetourAuMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueRechercheMotcle.this);
			}
		});
		btnRetourAuMenu.setBounds(151, 518, 146, 23);
		contentPane.add(btnRetourAuMenu);		
		
		JLabel lblOuvrages = new JLabel("Ouvrages");
		lblOuvrages.setBounds(10, 153, 76, 14);
		contentPane.add(lblOuvrages);
		
		JLabel lblArticles = new JLabel("Articles");
		lblArticles.setBounds(254, 153, 76, 14);
		contentPane.add(lblArticles);
		
		textArea = new JTextArea();
		textArea.setBackground(Color.WHITE);
		textArea.setEnabled(false);
		textArea.setEditable(false);
		textArea.setBounds(10, 387, 427, 107);
		contentPane.add(textArea);
		
	}
	
	public String getText() {
		return comboBox.getSelectedItem().toString();
	}
}